package com.hermit.qmst.UI.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.hermit.qmst.R;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.Log;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Created by hermithermit on 15/2/18.
 */
public class SystemActivity extends Activity implements View.OnClickListener{

    private TextView mTextView;
    private ProgressDialog mProgressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_explain);
        initView();
        request();
    }

    private void initView(){
        ((TextView)findViewById(R.id.tv_title)).setText("系统通知");
        ((ImageView)findViewById(R.id.iv_title_left)).setImageResource(R.drawable.ic_back);
        findViewById(R.id.iv_title_left).setOnClickListener(this);

        findViewById(R.id.rlyt_chongzi).setOnClickListener(this);
        findViewById(R.id.rlyt_zifei).setOnClickListener(this);
        findViewById(R.id.rlyt_help).setOnClickListener(this);

        mTextView = (TextView)findViewById(R.id.tv_content);
        mTextView.setMovementMethod(new ScrollingMovementMethod());
    }

    private void request(){
        mProgressDialog = ProgressDialog.show(this, "提示", "系统正在处理，请稍后…", false, false);
        RequestQueue requestQueue = Volley.newRequestQueue(this);



        String url = Common.submitQueryTextPath();

        StringRequest request = new StringRequest(Request.Method.POST,url,new Response.Listener<String>(){

            @Override
            public void onResponse(String s) {
                Log.i("SystemActivity", s, true);
                if (s != null && s.length() > 0){

                   try{

                       JSONObject jsonObject = new JSONObject(s);

                       if (jsonObject.getString("status").equals("1")){
                           JSONArray dataArr = jsonObject.getJSONArray("data");
                           JSONObject item = dataArr.getJSONObject(0);
                           mTextView.setText(item.getString("content"));
                       }



                   }catch (Exception e){

                   }



                }

                mProgressDialog.dismiss();
            }
        },new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError volleyError) {

                mProgressDialog.dismiss();
            }
        }){
            @Override
            public byte[] getBody() {
                String requestParam =  "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data><type>systeminfo</type><uid>"+Common.mInfoId+"</uid><phone>"+ Common.myPhone +"</phone></data>";
                byte[] b = null;
                try {
                    b = requestParam.getBytes("utf-8");
                }catch (Exception e){
                    b = requestParam.getBytes();
                }
                return b;
            }
        };


        requestQueue.add(request);


    }


    @Override
    public void onClick(View v) {

        Intent intent;

        switch (v.getId()){

            case R.id.rlyt_chongzi:

                intent = new Intent(SystemActivity.this,ExplainDetailActivity.class);
                intent.putExtra("title","充值说明");
                startActivity(intent);
                break;

            case R.id.rlyt_zifei:
                intent = new Intent(SystemActivity.this,ExplainDetailActivity.class);
                intent.putExtra("title","资费说明");
                startActivity(intent);
                break;

            case R.id.rlyt_help:
                intent = new Intent(SystemActivity.this,ExplainDetailActivity.class);
                intent.putExtra("title","帮助中心");
                startActivity(intent);
                break;

            case R.id.iv_title_left:
                SystemActivity.this.finish();
                break;

            default:

                break;
        }

    }
}
